library(hydroGOF)
library(clusterSim)
library(DAAG)

data=read.csv("C:/data/myd_merge3.csv")
data=subset(data,data$station!=3 & data$station!=2 & data$station!=5)

data$aod=data.Normalization(data$aod,type="n5",normalization="column")
data$temp=data.Normalization(data$temp,type="n5",normalization="column")
data$avg_temp=data.Normalization(data$avg_temp,type="n5",normalization="column")
data$avg_rh=data.Normalization(data$avg_rh,type="n5",normalization="column")
data$avg_preci_24=data.Normalization(data$avg_preci_24,type="n5",normalization="column")


model=lm(pm25~aod+temp+avg_temp+avg_rh+avg_preci_24,data)

nSample=nrow(data)
nPredict= length(model$coefficients)-1
cutoff=4/(nSample-nPredict-1)
cookValue=cooks.distance(model)
data["cookValue"] = cookValue


data1=subset(data,data$cookValue<=cutoff)

model1=lm(pm25~aod+temp+avg_temp+avg_rh+avg_preci_24,data1)
modcv=cv.lm(df=data1,model1,m=10)

model_re=sum(abs(data1$pm25-modcv$cvpred)/data1$pm25)/nrow(data1)*100
model_rmse=rmse(data1$pm25,modcv$cvpred)
model_r=cor(data1$pm25,modcv$cvpred)

print(paste("Samples:",nrow(data1)))
print(paste("R2:",round(model_r*model_r,3)))
print(paste("RMSE:",round(model_rmse,3)))
print(paste("RE:",round(model_re,3)))

print("------------------")
	
	
	
